﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using virtuaPol.Main;
using virtuaPol.Model;


namespace virtuaPol.Net
{
    /// <summary>
    /// Interaction logic for LoadGameDlg.xaml
    /// </summary>
    public partial class LoadGameDlg : Window
    {
        IList<string> games = new List<string>();

        private CWorld world;
        public CWorld World { get { return world; } set { world = value; } }

        public bool IsCanceled {get;set;}

        public LoadGameDlg()
        {
            IsCanceled = true;
            if (System.IO.Directory.Exists(App.SAVEGAME_PATH))
            {
                String[] files = System.IO.Directory.GetFiles(App.SAVEGAME_PATH, "*"+App.WORLD_FILE_EXTENSION);
                foreach (String f in files)
                {
                    games.Add(System.IO.Path.GetFileNameWithoutExtension(f));
                }
            }
            InitializeComponent();
            if (games.Count == 0)
            {
                GameList.IsEnabled = false;
                GameList.Items.Add("There are no saved games!");
                BtLoadGame.IsEnabled = false;
            }
            else GameList.ItemsSource = games;
        }

        private void BtLoadGame_Click(object sender, RoutedEventArgs e)
        {
            if (GameList.SelectedItem == null)
            {
                MessageBox.Show("You have to select a saved game first.");
            }
            else
            {
                WorldLoader.load(out world,
                    App.SAVEGAME_PATH + (string)GameList.SelectedItem + App.WORLD_FILE_EXTENSION);
                IsCanceled = false;
                this.Close();
            }
        }

        private void BtCancel_Click(object sender, RoutedEventArgs e)
        {
            IsCanceled = true;
            this.Close();
        }
    }
}
